<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="java.util.List" %>
<%@ page import="model.User" %>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<h1>用户列表</h1>
<a href="admin.jsp">返回后台首页</a>
<hr>

<h2>添加新用户</h2>
<form action="UserServlet" method="post">
    <input type="hidden" name="action" value="add">
    <label>用户名: <input type="text" name="username" required></label>
    <label>密码: <input type="password" name="password" required></label>
    <input type="submit" value="添加用户">
</form>

<h2>用户列表</h2>
<table>
    <tr>
        <th>用户名</th>
        <th>密码</th>
        <th>操作</th>
    </tr>

    <%
        List<User> users = (List<User>) request.getAttribute("users");
        if (users != null) {
            for (User user : users) {
    %>
    <tr>
        <td><%= user.getUsername() %></td>
        <td><%= user.getPassword() %></td>
        <td>
            <form action="UserServlet" method="post" style="display:inline;">
                <input type="hidden" name="action" value="delete">
                <input type="hidden" name="id" value="<%= user.getId() %>">
                <input type="submit" value="删除">
            </form>
            <form action="UserServlet" method="post" style="display:inline;">
                <input type="hidden" name="action" value="update">
                用户名: <input type="text" name="username" value="<%= user.getUsername() %>">
                密码: <input type="password" name="password" value="<%= user.getPassword() %>">
                <input type="submit" value="修改">
            </form>
        </td>
    </tr>
    <%
        }
    } else {
    %>
    <tr>
        <td colspan="3">暂无用户记录。</td>
    </tr>
    <%
        }
    %>
</table>
</body>
</html>
